package com.imooc.mall.service.impl;

import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.dao.CartMapper;
import com.imooc.mall.model.dao.ProductMapper;
import com.imooc.mall.model.pojo.Cart;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.model.vo.CartVo;
import com.imooc.mall.service.CartService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("cartService")
public class  CartServiceImpl implements CartService {
    @Resource
    private ProductMapper productMapper;

    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<CartVo> add(Integer userId, Integer productId, Integer count){
        vaildProduct(productId,count);

        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if(cart == null){
            // 证明这个购物车中没有这个对象需要重新赋值,需要新增一个记录。
            cart = new Cart();
            cart.setProductId(productId);
            cart.setQuantity(count);
            cart.setUserId(userId);
            cart.setSelected(Constant.Check.selected);
            cartMapper.insertSelective(cart);
        }else{
            // 证明这个对象已经存在了 就对这个对象进行更新
            int countNew = cart.getQuantity() + count;
            Cart cartNew = new Cart();
            cartNew.setId(cart.getId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setQuantity(countNew);
            cartNew.setProductId(productId);
            cartNew.setSelected(Constant.Check.selected);
            cartMapper.updateByPrimaryKeySelective(cartNew);

        }
        return  this.list(userId);
    }

    @Override
    public List<CartVo> list(Integer UserId){
        /*查询所有的用户的购物车，经过重新构建的对象对这个请求的返回方法进行 重新构建
        在数据库中经过别名字 对数据进行重新组合起来，*/
        List<CartVo> cartVos = cartMapper.selectCartList(UserId);
        // 返回这个对象中的
        for (int i = 0; i < cartVos.size(); i++) {
            CartVo cartVo = cartVos.get(i);
            cartVo.setTotalPrice(cartVo.getPrice()*cartVo.getQuantity());
        }
        return cartVos;
    }


    private void vaildProduct(Integer productId, Integer count) {
        Product product = productMapper.selectByPrimaryKey(productId);
        // 判断商品是否存在，商品是否上架
        if (product == null || product.getStatus().equals(Constant.SaleStatus.NOT_SALE))
        {
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE);
        }

        // 如果要添加的数量大于数据库的数量是本能成功的
        if (count > product.getStock()){
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_ENOUGH);
        }
    }

    @Override
    public List<CartVo> update(Integer userId, Integer productId, Integer count){
        vaildProduct(productId,count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId,productId);
        if(cart == null){
            // 这个商品之前不在购物车，无法更新
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }else {
            // 这个商品在购物车里，则更新数量
            Cart cartNew = new Cart();
            cartNew.setQuantity(count);
            cartNew.setId(cart.getId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setSelected(Constant.Check.selected);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> delete(Integer userId, Integer product){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, product);
        if (cart == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        else {
            int i = cartMapper.deleteByPrimaryKey(cart.getId());
            if (i == 0){
                throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
            }
        }
        return this.list(userId);

    }

    @Override
    public List<CartVo> selectOrNot(Integer userId, Integer product, Integer selected){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, product);
        if (cart == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
        else {
            cartMapper.updateBySelectde(userId, product, selected);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> selectAll(Integer userId, Integer selected){
        // 改变选中的状态
        cartMapper.updateBySelectde(userId,null,selected);
        return this.list(userId);
    }
}